package com.aitaoxue.palx.fragment;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import butterknife.ButterKnife;
import butterknife.InjectView;

import com.aitaoxue.palx.R;
import com.aitaoxue.palx.base.BaseFragment;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

/**
 * app banners的具体展示页
 * 
 * @author earthgee
 * 
 */
@SuppressLint("ValidFragment")
public class ScreenSlidePageFragment extends BaseFragment {

	@InjectView(R.id.banner_item_iv)
	ImageView img;

	private int index;
	
	private String url=null;
	
	//记录是否加载过图片
	private boolean isLoadedImg=false;
	
	public ScreenSlidePageFragment() {
	}
	
	public ScreenSlidePageFragment(int index) {
		this.index=index;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.banner_item, null);

		ButterKnife.inject(this, view);
		
		isLoadedImg=false;
		changeImg();

		return view;
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void initView(View view) {
		// TODO Auto-generated method stub

	}

	@Override
	public void initData() {
		// TODO Auto-generated method stub

	}

	public int getIndex() {
		return index;
	}
	
	public void setUrl(String url){
		this.url=url;
		//changeImg();
	}
	
	public void changeImg(){
		if(url!=null&&img!=null&&!isLoadedImg){
			isLoadedImg=true;
			ImageLoader.getInstance().loadImage(url, new ImageLoadingListener() {
				
				@Override
				public void onLoadingStarted(String arg0, View arg1) {
				}
				
				@Override
				public void onLoadingFailed(String arg0, View arg1, FailReason arg2) {
				}
				
				@Override
				public void onLoadingComplete(String arg0, View arg1, Bitmap bitmap) {
					img.setImageBitmap(bitmap);
				}
				
				@Override
				public void onLoadingCancelled(String arg0, View arg1) {
				}
			});
		}
	}

}
